package com.maslyk.invitation.exception;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

public class ErrorHandler
{
	Logger log = Logger.getLogger(ErrorHandler.class);
	
    public void handleException(FacesContext fc, Exception ex)
    {
    	serviceException(fc,ex);
    }

    public void handleThrowable(FacesContext fc, Throwable ex)
    {
    	serviceException(fc,ex);
    }
    
    private void serviceException(FacesContext fc, Throwable ex)
    {
        log.error(ex,ex);
    	FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setSummary(ex.getMessage());
        message.setDetail(ex.getLocalizedMessage());
        
        fc.addMessage(null, message);
        try 
        {
			fc.getExternalContext().redirect(fc.getExternalContext().getRequestContextPath()+"/faces/index.jsp");
		} 
        catch (Exception e) 
		{
			log.error(ex,ex);
		}
    }
}
